<?php		
  require_once $_SERVER['DOCUMENT_ROOT'] . '/proyecto/clases/autoload.inc.php';		
  require_once ($_SERVER['DOCUMENT_ROOT'] .'/proyecto/modulos/informes/config/lang/eng.php');		
  require_once ($_SERVER['DOCUMENT_ROOT'] . '/proyecto/modulos/informes/tcpdf.php');		
  if (Zen::IniciarSesion()){		
     $fecha1 = stripslashes(htmlspecialchars(trim($_GET['fecha1'])));
    $fecha2 = stripslashes(htmlspecialchars(trim($_GET['fecha2'])));
     $rs = Zen::Consulta("SELECT 	UPPER(CON.CON_NOMBRE) AS NOMBRE_CONVENIO, 
	COUNT(EXOT.EXOT_ID) AS TOTAL_EXAMENES 
FROM	CONVENIOS AS CON JOIN EXAMENES_OT AS EXOT ON EXOT.CON_RUT= CON.CON_RUT
	JOIN ORDENES_TRABAJO AS OT ON OT.OT_ID=EXOT.OT_ID
WHERE 	OT_FECHA_ING >= '$fecha1' AND OT_FECHA_ING <='$fecha2'
GROUP BY CON.CON_NOMBRE
ORDER BY TOTAL_EXAMENES
LIMIT 10");
/**		
 * Creates an example PDF TEST document using TCPDF		
 * @package com.tecnick.tcpdf		
 * @abstract TCPDF - Example: Custom Header and Footer		
 * @author Nicola Asuni		
 * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com		
 * @link http://tcpdf.org		
 * @license http://www.gnu.org/copyleft/lesser.html LGPL		
 * @since 2008-03-04		
 */	
class MYPDF extends TCPDF {
	public function Header() {
	   $fecha=date("d-m-Y H:i:s");
	   $image_file = K_PATH_IMAGES.'logo.png';
	   $this->Image($image_file, 10, 10, 15, '', 'PNG', '', 'T', false, 0, '', false, false, 0, false, false, false);
       $this->SetFont('times', 'B', 8);
       $this->SetXY(25,11);
        $this->Cell( 0, 0, 'Centro de Imagenología S.A.', 0, false, 'L', 0, '', 0, false, 'M', 'M');
        $this->SetXY(25,15);
        $this->Cell( 0, 0, 'Administración y Finanzas', 0, false, 'L', 0, '', 0, false, 'M', 'M');
       $this->SetXY(25,9.5);
       $this->Cell( 0, 0, $fecha, 0, false, 'R', 0, '', 0, false, 'M', 'M');
       $this->SetXY(25,14);
       $this->Cell( 0, 0, $_SESSION['usuario'], 0, false, 'R', 0, '', 0, false, 'M', 'M');
       $this->ln(4.5);
       $this->Cell(190, 0, 'Página '.$this->getAliasNumPage().' de '.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
	}
	public function Footer() {
	}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 003');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', 'BN', 12);
$pdf->AddPage();
$txt = <<<EOD
Ranking Convenios
EOD;
$pdf->SetFont('times', 'B', 14);
$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
$pdf->ln(4);
$pdf->SetFont('times', 'B', 10);
$pdf->Cell( 40, 0, 'Desde', 0, false, 'R', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 10);
$pdf->Cell( 40, 0, $fecha1, 0, false, 'L', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', 'B', 10);
$pdf->Cell( 40, 0, 'Hasta', 0, false, 'R', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 10);
$pdf->Cell( 40, 0, $fecha2, 0, false, 'L', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 10);
$pdf->AddSpotColor('Pantone 116 C', 0, 20, 100, 0);
$pdf->AddSpotColor('HKS 16 K', 30, 100, 90, 10);
$pdf->AddSpotColor('Pantone 505 C', 57, 100, 85, 55);
$pdf->AddSpotColor('Pantone 440 C', 50, 60, 80, 70);
$pdf->AddSpotColor('Pantone 288 C', 100, 60, 10, 5);
$pdf->AddSpotColor('Pantone 289 C', 100, 78, 50, 0);
$pdf->AddSpotColor('Pantone 356 C', 100, 30, 100, 0);
$pdf->AddSpotColor('Pantone 567 C', 100, 50, 80, 45);
$pdf->AddSpotColor('Pantone 9060 C', 0, 0, 7, 0);
$pdf->AddSpotColor('Pantone 420 C', 22, 14, 22, 0);
$pdf->AddSpotColor('Pantone 422 C', 39, 24, 34, 0);
$pdf->AddSpotColor('Pantone 433 C', 34, 0, 0, 94);
$pdf->AddSpotColor('NovaSpace-Black', 50, 0, 0, 100);
$pdf->AddSpotColor('Pantone 601 C', 0, 0, 55, 0);
$pdf->AddSpotColor('Pantone 659 C', 50, 20, 0, 10);
$colores=array(
0=> "Pantone 116 C",
1 => "HKS 16 K",
2 => "Pantone 505 C",
3 => "Pantone 440 C",
4 => "Pantone 288 C",
5 => "Pantone 289 C",
6 => "Pantone 356 C",
7 => "Pantone 567 C",
8=> "Pantone 9060 C",
9 => "Pantone 420 C",
10 => "Pantone 422 C",
11 => "Pantone 433 C",
12 => "NovaSpace-Black",
13 => "Pantone 601 C",
14 => "Pantone 659 C");
$pdf->ln(10);
$total=0;
$aux=0;
$aux2=0;
$vect=array();
$vect2=array();
$vect[0]=0;
$vect2[0]="";
$cont=0;
$cont2=0;
foreach ($rs as $k => $record){
$cont++;
$total=$total+$record['total_examenes'];
$vect[$cont]=$record['total_examenes'];
$vect2[$cont]=$record['nombre_convenio'];
}
$cortador=250/$total;
for($i=0;$i<=$cont;$i++){
   $vect[$i]=$vect[$i]*$cortador;
}
$xc = 85;
$yc = 80;
$r = 30;
$starty = 60;
if($cont==1){
    $pdf->SetFillSpotColor($colores[0], 100);
    $pdf->PieSector($xc, $yc, $r, 0, 360, 'FD', false, 0, 2);
    $pdf->SetFillSpotColor($colores[0], 100);
    $pdf->Rect(130, $starty, 4, 4, 'DF');
    $pdf->SetXY(136,$starty);
    $pdf->Cell( 35, 0, $vect2[0], 0, false, 'L', 0, '', 0, false, 'M', 'M');
    }
if($cont>1){
    for($i=1;$i<=$cont;$i++){
        $cont2++;
            if($cont>$i && $cont!=$cont2){
                $vect[$i]=$vect[$i]+$vect[$i-1];
                $starty+=5;
                $pdf->SetFillSpotColor($colores[$i], 100);
                $pdf->PieSector($xc, $yc, $r, $vect[$i-1], $vect[$i], 'FD', false, 0, 2);
                $pdf->SetFillSpotColor($colores[$i], 100);
                $pdf->Rect(130, $starty, 4, 4, 'DF');
                $pdf->SetXY(136,$starty);
                $pdf->Cell( 35, 0, $vect2[$i], 0, false, 'L', 0, '', 0, false, 'M', 'M');
        }
        else if($cont==$cont2) {
            $starty+=5;
            $pdf->SetFillSpotColor($colores[$i], 100);
            $pdf->PieSector($xc, $yc, $r, $vect[$i-1], 0, 'FD', false, 0, 2);
            $pdf->SetFillSpotColor($colores[$i], 100);
            $pdf->Rect(130, $starty, 4, 4, 'DF');
            $pdf->SetXY(136,$starty);
            $pdf->Cell( 35, 0, $vect2[$i], 0, false, 'L', 0, '', 0, false, 'M', 'M');
        }        
    }
    }
$rs = Zen::Consulta("SELECT 	UPPER(CON.CON_NOMBRE) AS NOMBRE_CONVENIO, 
	COUNT(EXOT.EXOT_ID) AS TOTAL_EXAMENES 
FROM	CONVENIOS AS CON JOIN EXAMENES_OT AS EXOT ON EXOT.CON_RUT= CON.CON_RUT
	JOIN ORDENES_TRABAJO AS OT ON OT.OT_ID=EXOT.OT_ID
WHERE 	OT_FECHA_ING >= '$fecha1' AND OT_FECHA_ING <='$fecha2'
GROUP BY CON.CON_NOMBRE
ORDER BY TOTAL_EXAMENES DESC
LIMIT 10");
$pdf->ln(57);
$pdf->SetX(40);
$pdf->Cell( 65, 0, 'Convenio', 1, false, 'C', 0, '', 1, false, 'M', 'M');
$pdf->Cell( 35, 0, 'Cantidad de Examenes', 1, false, 'C', 0, '', 1, false, 'M', 'M');
$pdf->ln(4.5);
foreach ($rs as $k => $record){
    $pdf->SetX(40);
    $pdf->Cell( 65, 0, $record['nombre_convenio'], 1, false, 'L', 0, '', 1, false, 'M', 'M');
    $pdf->Cell( 35, 0, $record['total_examenes'], 1, false, 'C', 0, '', 1, false, 'M', 'M');
    $pdf->ln(4.5);
}
$pdf->SetX(40);
$pdf->Cell( 65, 0, 'Total', 1, false, 'C', 0, '', 1, false, 'M', 'M');
$pdf->Cell( 35, 0, $total, 1, false, 'C', 0, '', 1, false, 'M', 'M');
ob_end_clean();
$pdf->Output('r29.pdf', 'D');
} else {
    Zen::LogoutModulo();
}
?>